﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Globalization;
using Public;
using System.Data;

namespace WebsiteAdmin.Salomon.ProInfo.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class CatList : IHttpHandler
    {
        public DataTable dtRtn = new DataTable();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string rtn = "";

            if (Cookie.AdminLoginName != null || Cookie.AdminLoginName != "")
            {
                DataTable dt = Bll.Category.MULTIPLE(Global.ConSalomon);
                if (dt.Rows.Count > 0)
                {

                    // Add three column objects to the table.
                    DataColumn dc1 = new DataColumn();
                    dc1.DataType = System.Type.GetType("System.Int32");
                    dc1.ColumnName = "PKID";
                    //dc1.AutoIncrement = true;
                    dtRtn.Columns.Add(dc1);

                    DataColumn dc2 = new DataColumn();
                    dc2.DataType = System.Type.GetType("System.String");
                    dc2.ColumnName = "CatName";
                    //dc2.AutoIncrement = true;
                    dtRtn.Columns.Add(dc2);

                    DataColumn dc3 = new DataColumn();
                    dc3.DataType = System.Type.GetType("System.Int32");
                    dc3.ColumnName = "ParID";
                    //dc3.AutoIncrement = true;
                    dtRtn.Columns.Add(dc3);

                    DataColumn dc4 = new DataColumn();
                    dc4.DataType = System.Type.GetType("System.Int32");
                    dc4.ColumnName = "CatRank";
                    //dc4.AutoIncrement = true;
                    dtRtn.Columns.Add(dc4);

                    DataColumn dc5 = new DataColumn();
                    dc5.DataType = System.Type.GetType("System.Int32");
                    dc5.ColumnName = "SortBy";
                    //dc5.AutoIncrement = true;
                    dtRtn.Columns.Add(dc5);

                    GetCatData(0, dt, dtRtn);



                    //rtn += "{\"PKID\":\"" + dt.Rows[i]["PKID"].ToString() + "\",\"CatName\":\"" + dt.Rows[i]["CatName"].ToString() + "\"},";
                }
                //}
                dt.Dispose();
                //rtn = rtn.Substring(0, rtn.Length - 1);
            }
            rtn = "[" + rtn + "]";
            context.Response.Write(rtn);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private bool GetCatData(int parID, DataTable dtCat, DataTable dtRtn)
        {


            DataRow[] drs = dtCat.Select(" [ParID] = " + parID);

            for (int i = 0; i < drs.Length; i++)
            {
                DataRow dr = dtRtn.NewRow();
                dr["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                dr["CatName"] = drs[i]["CatName"].ToString();
                dr["ParID"] = Convert.ToInt32(drs[i]["ParID"]);
                dr["CatRank"] = Convert.ToInt32(drs[i]["CatRank"]);
                dr["SortBy"] = Convert.ToInt32(drs[i]["SortBy"]);
                dtRtn.Rows.Add(dr);
                GetCatData(Convert.ToInt32(drs[i]["PKID"]), dtCat, dtRtn);
            }
            return true;
        }

    }
}
